package com.kgd.news.ibeetl.tag;

import com.kgd.news.entity.SysDict;
import org.beetl.core.Tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/6/21
 * @Time: 下午4:12
 */
public class DictTag extends Tag {
//

//    点击资讯管理  调到：DictTAG Render
//    val -1
//    type select
//    group yes_no
//    pname isbanner
//    class  ""
//    style  margin-left:5px
//    defaultNumber 6
//select id="select1" name="select1" onchange="outputSelect();">
//<select id="id_yes_no" name="isbanner" class="" style="margin-left:5px;" ><option value="">请选择</option><option value="1">是</option><option value="2">否</option></select>

    @Override
    public void render() {
        try {
                    Map attrs = (Map) args[1];
            String selVal = attrs.get("val") == null ? "-1" : (attrs.get("val") +  "");
            String type = attrs.get("type") + "";
            String group = attrs.get("group") + "";
            String pname = attrs.get("pname") + "";
            String cls = attrs.get("class") == null ? "" : attrs.get("class") + "";
            String style = attrs.get("style") == null ? "" : attrs.get("style") + "";
            String defaultNumber = attrs.get("defaultNumber") + "";
            if ("select".equals(type)) {
                ctx.byteWriter.writeString(select(selVal, group, pname, cls, style, defaultNumber));
            } else if ("radio".equals(type)) {
                ctx.byteWriter.writeString(radio(selVal, group, pname, cls, style, defaultNumber));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 下拉标签
     * @param selVal
     * @param group
     * @param class_
     * @param style
     * @param defaultnumber
     * @return
     */
    private String select(String selVal, String group, String pname,
                          String class_, String style, String defaultnumber) {
        StringBuilder sb = new StringBuilder();
        /*if (StrKit.notBlank(id)) {
            sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
            sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" >");
        } else {*/
        sb.append("<select id=\"").append("id_" + group).append("\" name=\"").append(pname);

//        sb.append("\" onchange=\"").append("outputSelect()");
        sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" >");
        /*sb.append("<select id=\"\" name=\"").append(pname).append("\" class=\"").append(class_);
        sb.append("\" style=\"").append(style).append("\" >");*/
        // }
        sb.append("<option value=\"").append("").append("\">");
        sb.append("请选择");
        sb.append("</option>");

        List<SysDict> dictList = SysDict.dao.find("select * from sys_dict d where d.d_group = ?", group);
        for (SysDict dl : dictList) {
            String value = dl.getDValue();
            String name = dl.getName();
            if ((null != selVal && (selVal.equals(value)) || (("-1").equals(selVal)&&value.equals(defaultnumber)))) {// 默认选中
                sb.append("<option value=\"").append(value).append("\" selected=\"selected\">");
                sb.append(name);
                sb.append("</option>");
            }  else{
                sb.append("<option value=\"").append(value).append("\">");
                sb.append(name);
                sb.append("</option>");
            }
        }
        sb.append("</select>");

        return sb.toString();
    }

    private String radio(String selVal, String group, String pname, String class_, String style, String defaultnumber) {
        StringBuilder sb = new StringBuilder();

        SysDict dict = SysDict.dao.findFirst("select * from sys_dict where d_group = ? and d_value = ?", group, selVal);

        List<SysDict> dictList = SysDict.dao.find("select * from sys_dict d where d.d_group = ?", group);

        for (SysDict d : dictList) {
            String value = d.getDValue();
            String name = d.getName();
            if ((dict != null && dict.getDValue().equals(value)) || value.equals(defaultnumber)) {// 默认选中
                sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + name)
                        .append("<input type=\"radio\" id=\"")
                        .append(value)
                        .append(dictList.indexOf(d))
                        .append("\" name=\"")
                        .append(pname)
                        .append("\" value=\"")
                        .append(value)
                        .append("\" class=\"")
                        .append(class_)
                        .append("\" style=\"")
                        .append(style)
                        .append("\" checked=\"checked\" >");

            } else {
                sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + name)
                        .append("<input type=\"radio\" id=\"")
                        .append(value)
                        .append(dictList.indexOf(dict))
                        .append("\" name=\"")
                        .append(pname)
                        .append("\" value=\"")
                        .append(value)
                        .append("\" class=\"")
                        .append(class_)
                        .append("\" style=\"")
                        .append(style)
                        .append("\" >");
            }
        }
        //sb.append("</select>");
        return sb.toString();
    }

}
